Skip to content

Make #[track_caller] async fn track the caller, not the poller/awaiter - #163396

Open
theemathas wants to merge 8 commits into
rust-lang:mainfrom
theemathas:async-track-caller-not-poller
Open

theemathas wants to merge 8 commits into
rust-lang:mainfrom
theemathas:async-track-caller-not-poller

Conversation

@theemathas

@theemathas theemathas commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

An LLM was used to locate relevant code, suggest ideas, and review the code. However, I manually wrote all code, and I take all responsibility for all code written and all decisions made.

Tracking issue (async_fn_track_caller): #110011

Related tracking issue (closure_track_caller): #87417

The first commit adds extensive tests for behavior touched in this PR. Commits 2-7 are the actual implementation, and also modification of the tests to reflect this new behavior.

This PR makes coroutines desugared from #[track_caller] async fn track the caller of the function, not the poller/awaiter of the coroutine. This is done as per T-lang's decision at #110011 (comment). The implementation modifies AST lowering to add an upvar to the coroutine, which stores the relevant caller location.

In both codegen and in consteval/miri, we read the relevant upvar from the coroutine at the point in time where we need access to the &Location reference (either when we call the caller_location intrinsic, when we panic, or when we call another #[track_caller] function). We only dereference this reference as needed, when we actually need the Location information.

This PR changes the behavior of async fn, but not of async closures, due to difficulties I've noted at #t-compiler/help > Help with `async_fn_track_caller` @ 💬

This PR's implementation assumes that the desired behavior in #110011 (comment) is the behavior that I think makes the most sense.

This PR corrects the behavior of #163406 when the async_fn_track_caller feature is enabled, but does not give a warning when the feature is disabled (resulting in the pre-existing broken behavior).

The tests pass on cranelift when I run ./x test ui --test-codegen-backend cranelift -- async-await/track-caller/panic-track-caller-no-unwind.rs. (Cranelift doesn't support unwinding.)

r? compiler

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 27, 2026
@theemathas
theemathas force-pushed the async-track-caller-not-poller branch 3 times, most recently from f21a754 to 7e01c99 Compare September 28, 2026 15:47
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from 7e01c99 to c87bff0 Compare September 29, 2026 11:52
Comment on lines +988 to +997
if is_in_trait_impl {
// Check if we need to "inherit" #[track_caller] from the trait definition.
let Some(trait_item_def_id) =
self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
else {
self.dcx().span_delayed_bug(span, "could not resolve trait item being implemented");
return false;
};
return find_attr!(self.tcx, trait_item_def_id, TrackCaller(_));
}

@theemathas theemathas Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this is doing, but I'm doing the same thing as this existing code:

let (effective_ident, impl_kind) = if is_in_trait_impl {
let trait_item_def_id = self
.get_partial_res(i.id)
.and_then(|r| r.expect_full_res().opt_def_id())
.ok_or_else(|| {
self.dcx()
.span_delayed_bug(span, "could not resolve trait item being implemented")
});

View changes since the review

Comment thread compiler/rustc_mir_transform/src/check_inline.rs
Comment on lines +108 to 110
///
/// FIXME(async_fn_track_caller): What if the Location is stored inside a coroutine upvar?
fn resolve_tracked_call_location(body: &Body, inherited: Option<MirConst>) -> MirConst {

@theemathas theemathas Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this, and do I need to modify it?

View changes since the review

@theemathas
theemathas marked this pull request as ready for review September 29, 2026 12:19
@rustbot

rustbot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_attr_ir

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 29, 2026
Comment thread compiler/rustc_const_eval/src/interpret/eval_context.rs Outdated
Comment thread compiler/rustc_const_eval/src/interpret/operand.rs Outdated
@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from c87bff0 to b14e6ed Compare September 29, 2026 13:07
Comment thread compiler/rustc_const_eval/src/interpret/intrinsics.rs Outdated
@theemathas

This comment was marked as resolved.

Comment thread compiler/rustc_codegen_ssa/src/mir/block.rs
@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from b14e6ed to e1d818f Compare October 1, 2026 13:57
@rustbot

This comment has been minimized.

@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from e1d818f to 6fcdc09 Compare October 1, 2026 14:56
@theemathas

Copy link
Copy Markdown
Contributor Author

I split up the implementation into multiple commits, hopefully making reviewing easier. (And also hopefully making it less confusing for me.)

@theemathas
theemathas force-pushed the async-track-caller-not-poller branch 2 times, most recently from 6fcdc09 to 473dbf0 Compare October 1, 2026 15:01
@theemathas theemathas changed the title Make #[track_caller] async fn track the caller, not the poller Make #[track_caller] async fn track the caller, not the poller/awaiter Oct 2, 2026
@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from 473dbf0 to 57abba7 Compare October 2, 2026 11:02
@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from f857e66 to b3015fc Compare October 19, 2026 02:48
@rustbot

This comment has been minimized.

@theemathas

theemathas commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased, since #163746 was merged. In the process, the commit previously cherry-picked from there was removed.

Here's a possibly more useful range-diff

I'll address Ralf's comments later.

programskillforverification pushed a commit to programskillforverification/miri that referenced this pull request Oct 19, 2026
…fJung

consteval: Rework caller location handling

Instead of walking up the call stack each time we need the caller location, we emulate the run time behavior of passing an implicit caller location argument in `#[track_caller]` functions. We record a representation of that argument in each frame. For inlined functions though, we still do a "stack walk" within that one frame.

This PR is a prerequisite for rust-lang/rust#163396

Fixes rust-lang#5351
@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from b3015fc to e4d0827 Compare October 19, 2026 16:52
@rustbot

rustbot commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@theemathas

Copy link
Copy Markdown
Contributor Author

And rebased again after #163262 was merged.

@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from e4d0827 to fd39ca1 Compare October 23, 2026 14:08
@theemathas

Copy link
Copy Markdown
Contributor Author

I've addressed @RalfJung's comments

@theemathas

Copy link
Copy Markdown
Contributor Author

@RalfJung I've added a bunch of Miri tests, but the behavior doesn't make sense to me at all. Any suggestions on fixing these? 😅

@rust-log-analyzer

This comment has been minimized.

@theemathas

This comment was marked as resolved.

@RalfJung

This comment was marked as resolved.

@RalfJung

RalfJung commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

@RalfJung I've added a bunch of Miri tests, but the behavior doesn't make sense to me at all. Any suggestions on fixing these? 😅

What exactly doesn't make sense? What needs fixing?

For the tests that involve the aliasing model I'd suggest switching to Tree Borrows, it has fewer unintuitive corner cases.

//@compile-flags: -Zmiri-tree-borrows

@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from 9cdcb2d to 7bbcfe3 Compare October 24, 2026 15:15
@theemathas

Copy link
Copy Markdown
Contributor Author

@RalfJung I've rewritten the aliasing test, and edited some stuff.

Here are things I find confusing in the current state of the tests in src/tools/miri/tests/fail/async_fn_track_caller/:

  • The inconsistency between aliased-location-1.rs and aliased-location-2.rs seem to indicate that there's something special about the first poll of an async fn, which is odd.
  • I don't understand why null-location-ref.rs and uninit-location-ref are UB. We're only constructing references that point to invalid &Location references, right?

@rust-log-analyzer

This comment has been minimized.

@theemathas
theemathas force-pushed the async-track-caller-not-poller branch from 7bbcfe3 to aa9ffe4 Compare October 25, 2026 08:52
@RalfJung

Copy link
Copy Markdown
Member

I don't know the MIR being run here, it is generated by the async lowering. You are asking me to spend a lot of time reverse engineering the exact steps that the auto-generated MIR is taking here. Is that worth it? Do you for some reason not want that code to have UB?
My guess would be that the MIR actually loads that field at its reference type and therefore it's UB for that field to be null / dangling. You can dig into this more yourself with something like the following that will show a trace of each MIR statement being executed:

MIRI_LOG=info ./x run miri -- src/tools/miri/tests/fail/async_fn_track_caller/null-location-ref.rs

seem to indicate that there's something special about the first poll of an async fn

It looks like the caller location isn't read every single time the async fn is polled. It's apparently only read on the first poll and the third poll. I don't know much about the sync lowering so I can't tell you why that is the case.

I don't understand the point of these tests, to be honest. Why should we have such fragile tests that probe implementation details of the generator lowerings?

@theemathas

Copy link
Copy Markdown
Contributor Author

@RalfJung

Why should we have such fragile tests that probe implementation details of the generator lowerings?

I might have misunderstood your intentions. I thought that you wanted me to have the miri implementation of track_caller match the run time implementation so that Miri's UB always matches whatever the run time implementation assumes. Making sure that this is the case presumably requires fragile tests that reflect what things are assumed by the implementation. Am I misunderstanding what I was supposed to do?

@theemathas

Copy link
Copy Markdown
Contributor Author

A potential alternative is to, in miri, require that all caller locations inside #[track_caller] async fn coroutines point to miri-allocated caller-location-specific allocations. This would detect more UB than what the runtime needs, but would make explaining the UB easier, and would still allow most if not all sane uses of such coroutines. I'm not entirely sure though about when exactly this check should be done, if we go with this approach.

@theemathas

Copy link
Copy Markdown
Contributor Author

I just figured out why the tests behaved so weirdly. It's because I did a dumb and made a typed copy of the &Location references at 9e15722 (this PR). This typed copy runs on exactly the first poll of the coroutine. I'll get around to fixing this later, after which I expect the behaviour of stuff to make a lot more sense.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants